package com.nxbw.quality.check.service.impl;

import com.nxbw.quality.base.response.PageData;
import com.nxbw.quality.check.mapper.CheckMapper;
import com.nxbw.quality.check.pojo.dto.CheckQuery;
import com.nxbw.quality.check.pojo.dto.CheckSaveParam;
import com.nxbw.quality.check.pojo.entity.Check;
import com.nxbw.quality.check.pojo.vo.CheckVO;
import com.nxbw.quality.check.service.CheckService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class CheckServiceImpl implements CheckService {
    @Autowired
    private CheckMapper checkMapper;

    @Override
    public void saveCheck(CheckSaveParam checkSaveParam) {
        log.debug("保存检查业务: {}", checkSaveParam);
        Check check = new Check();
        BeanUtils.copyProperties(checkSaveParam, check);
        log.debug("copyProp: {}", check);
        if (checkSaveParam.getId() == null) {
            log.debug("新增");
            checkMapper.insertCheck(check);
        } else {
            log.debug("更新");
            checkMapper.updateCheck(check);
        }
    }

    @Override
    public void deleteCheck(Long id) {
        checkMapper.deleteCheckById(id);
    }

    @Override
    public PageData selectCheck(CheckQuery checkQuery) {
        log.debug("查询检查业务: {}", checkQuery);
        List<CheckVO> list = checkMapper.selectCheck(checkQuery);
        //准备PageData对象用来装返回给前端的数据
        PageData data = new PageData();
        //向PageData对象中装存入当前页字典数据
        data.setList(list);
        //向PageData对象中装存入总记录数
        data.setTotal(checkMapper.selectCountCheck(checkQuery));
        //返回封装好的对象
        return data;
    }
}
